package com.drought.rained.ui.activity.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.rxbus.EventMsgLJ;
import com.drought.rained.utils.ToastUtilsLJ;
import com.facebook.accountkit.AccountKitLoginResult;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;
import com.drought.rained.BuildConfig;
import com.drought.rained.R;
import com.drought.rained.LJApplication;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.mvpview.LoginMvpViewLJ;
import com.drought.rained.presenter.LoginPresenterLJ;
import com.drought.rained.response.LoginMethodResponseLJ;
import com.drought.rained.response.LoginResponseLJ;
import com.drought.rained.rxbus.EventsLJ;
import com.drought.rained.rxbus.RxBusLJ;
import com.drought.rained.utils.NoDoubleClickUtilsLJ;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crossoverone.statuslib.StatusUtil;

@SuppressLint("CheckResult")
@Route(name = "选择登录方式页面", path = RouterMapLJ.FACEBOOKLOGIN)
public class LoginMethodActivityLJ extends BaseTitleActivity implements LoginMvpViewLJ {
    public static int APP_REQUEST_CODE = 99;

    @BindView(R.id.iv_facebook_icon)
    ImageView iv_facebook_icon;
    @BindView(R.id.tv_login_by_facebook)
    Button tv_login_by_facebook;
    @BindView(R.id.login_by_user_and_psw)
    TextView login_by_user_and_psw;
    @BindView(R.id.display_show_text)
    TextView display_show_text; // 调试用

    private LoginPresenterLJ loginPresenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        loginPresenter = new LoginPresenterLJ(this);
        if (BuildConfig.DEBUG) {
            display_show_text.setVisibility(View.VISIBLE);
            login_by_user_and_psw.setVisibility(View.VISIBLE);
        }
//        else {
//            // 后台控制登陆
//            loginPresenter.getLoginMethod();
//        }
    }

    @Override
    protected void setSystemInvadeBlack() {
        StatusUtil.setUseStatusBarColor(this, getResources().getColor(R.color.colorTransparent));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_login_method;
    }

    @OnClick({R.id.iv_facebook_icon, R.id.tv_login_by_facebook, R.id.display_show_text, R.id.login_by_user_and_psw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_by_facebook:
            case R.id.iv_facebook_icon:
                phoneLogin();
                break;
            case R.id.display_show_text:
                if (display_show_text.getText() == null) return;
                doUploadAuthCode(display_show_text.getText().toString());
                break;
            case R.id.login_by_user_and_psw:
                ARouter.getInstance().build(RouterMapLJ.LOGIN_PAGE).navigation();
                finish();
                break;
        }
    }

    private void phoneLogin() {
        Intent intent = new Intent(LoginMethodActivityLJ.this, AccountKitActivity.class);
        AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(
                        LoginType.PHONE,
                        AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN

        intent.putExtra(
                AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
                configurationBuilder.build());
        startActivityForResult(intent, APP_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == APP_REQUEST_CODE) {
            AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
            String toastMessage;
            if (loginResult.getError() != null) {
                toastMessage = loginResult.getError().getErrorType().getMessage();
            } else if (loginResult.wasCancelled()) {
                toastMessage = "Login Cancelled";
            } else {
                toastMessage = "Success";

                if(BuildConfig.DEBUG) {
                    display_show_text.setText(loginResult.getAuthorizationCode());
                }
                doUploadAuthCode(loginResult.getAuthorizationCode());
            }

            // Surface the result to your user in an appropriate way.
            ToastUtilsLJ.showShort(LoginMethodActivityLJ.this, toastMessage);
        }
    }

    private void doUploadAuthCode(String authorizationCode) {
        if (NoDoubleClickUtilsLJ.isDoubleClick()) return;
        showLoadingView();
        loginPresenter.uploadFaceBookCode(authorizationCode);
    }

    @Override
    public void onGetDataNext(LoginMethodResponseLJ response) {
        if (response.getStatus() == ConstantLJ.HTTP_OK && response.hasData()) {
            // 2为显示 1为隐藏
//            if ("2".equals(response.getData().getFacebookLogin())) {
//                iv_facebook_icon.setVisibility(View.VISIBLE);
//                tv_login_by_facebook.setVisibility(View.VISIBLE);
//            } else {
//                iv_facebook_icon.setVisibility(View.GONE);
//                tv_login_by_facebook.setVisibility(View.GONE);
//            }
//            if ("2".equals(response.getData().getPasswordLogin())) {
//                login_by_user_and_psw.setVisibility(View.VISIBLE);
//            } else {
//                login_by_user_and_psw.setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public void onGetDataNext(LoginResponseLJ response) {
        dismissLoadingView();
        if (response.getStatus() == ConstantLJ.HTTP_OK) {
            AcacheLJ.get(LJApplication.getContext()).put(AcacheKeysLJ.TOKEN, response.getData().getToken());
            EventMsgLJ msg = new EventMsgLJ();
            msg.setMsg(EventsLJ.LOGIN_SUCCESS);
            RxBusLJ.getInstance().post(msg);
            finish();
        } else {
            ToastUtilsLJ.showShort(LoginMethodActivityLJ.this, response.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
